//
//  StartThreeViewController.m
//  yinsiwenzhen
//
//  Created by Yangfan on 14/12/24.
//  Copyright (c) 2014年 4gread. All rights reserved.
//

#import "StartThreeViewController.h"

@interface StartThreeViewController ()

@end

@implementation StartThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (_listView.howAboutArr != nil && _listView.howAboutArr.count > 0) {
        self.navigationItem.rightBarButtonItem.tintColor = HexRGB(0x2AADEA);
        [self.navigationItem.rightBarButtonItem setEnabled:YES];
    } else {
        self.navigationItem.rightBarButtonItem.tintColor = HexRGB(0xf00);
        [self.navigationItem.rightBarButtonItem setEnabled:NO];
    }
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"第三步";
    self.navigationItem.backBarButtonItem = backItem;
    
    if (isiphone6plus) {
        _destributionWord.font = [UIFont systemFontOfSize:24];
        _navigationTitle.font = [UIFont systemFontOfSize:20];
    } else {
        _destributionWord.font = [UIFont systemFontOfSize:20];
        _navigationTitle.font = [UIFont systemFontOfSize:19];
    }
    _mainTable.tableFooterView = [[UIView alloc] init];
    dateArr = @[@"瘙痒", @"阵痛", @"刺痛", @"麻木", @"灼热", @"有脓包", @"其他病症"];
}

#pragma mark - UITableViewDataSource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     if (isiphone6plus) {
         return 44;
     } else {
         return 36;
     }
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (dateArr != nil && dateArr.count > 0) {
        return dateArr.count;
    } else {
        return 10;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"threeViewControllerCell"];
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    
    // 重用机制，对于已经重用的view有影响
//    [(UILabel *)[cell viewWithTag:1] setTextColor:HexRGB(0xAFAEAE)] ;
//    [[cell viewWithTag:2] removeFromSuperview];
    
    UILabel *title = (UILabel *)[cell viewWithTag:1];
    title.text = [dateArr objectAtIndex:indexPath.row];
    if (isiphone6plus) {
        title.font = [UIFont systemFontOfSize:22];
    } else {
        title.font = [UIFont systemFontOfSize:18];
    }
    
    UIImageView *imageSelected = [[UIImageView alloc] init];
    if (isiphone6plus) {
        [imageSelected setFrame:CGRectMake(105, 0, 17, 44)];
    } else {
        [imageSelected setFrame:CGRectMake(105, 0, 14, 36)];
    }
    imageSelected.tag = 2;
    [imageSelected setHidden:YES];
    [imageSelected setImage:[UIImage imageNamed:@"selected"]];
    [cell.contentView addSubview:imageSelected];
    
    if (_listView.howAboutArr != nil && _listView.howAboutArr.count > 0) {
        for (NSString *str in _listView.howAboutArr) {
            int currentNum = [dateArr indexOfObject:str];
            if (currentNum < 100) {
                if (currentNum == indexPath.row) {
                    title.textColor = HexRGB(0x60cbfd);
                    [imageSelected setHidden:NO];
                    
                    NSIndexPath *ip = [NSIndexPath indexPathForRow:currentNum inSection:0];
                    [tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
                    
                }
            }
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *title = (UILabel *)[cell viewWithTag:1];
    [title setTextColor:HexRGB(0xAFAEAE)];
    [[cell viewWithTag:2] setHidden:YES];
    
    if ([_mainTable indexPathsForSelectedRows].count <= 0) {
        self.navigationItem.rightBarButtonItem.tintColor = HexRGB(0xf00);
        [self.navigationItem.rightBarButtonItem setEnabled:NO];
    } else {
        self.navigationItem.rightBarButtonItem.tintColor = HexRGB(0x2AADEA);
        [self.navigationItem.rightBarButtonItem setEnabled:YES];
    }
    
    [self getCurrentUserInput];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *title = (UILabel *)[cell viewWithTag:1];
    [title setTextColor:HexRGB(0x60cbfd)];
    [[cell viewWithTag:2] setHidden:NO];
    
    if ([_mainTable indexPathsForSelectedRows].count <= 0) {
        self.navigationItem.rightBarButtonItem.tintColor = HexRGB(0xf00);
        [self.navigationItem.rightBarButtonItem setEnabled:NO];
    } else {
        self.navigationItem.rightBarButtonItem.tintColor = HexRGB(0x2AADEA);
        [self.navigationItem.rightBarButtonItem setEnabled:YES];
    }
    
    [self getCurrentUserInput];
}

- (NSMutableArray *)getCurrentUserInput {
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (NSIndexPath *indexpath in [_mainTable indexPathsForSelectedRows]) {
        [arr addObject:[dateArr objectAtIndex:indexpath.row]];
    }
    _listView.howAboutArr = arr;
    NSLog(@"****%@", arr);
    return arr;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"toFourAction"]) {
        UIViewController *fourViewController = segue.destinationViewController;
        if ([fourViewController respondsToSelector:@selector(setListView:)]) {
//            NSMutableArray *arr = [[NSMutableArray alloc] init];
//            for (NSIndexPath *indexpath in [_mainTable indexPathsForSelectedRows]) {
//                [arr addObject:[dateArr objectAtIndex:indexpath.row]];
//            }
            _listView.howAboutArr = [self getCurrentUserInput];
            [fourViewController setValue:_listView forKey:@"listView"];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
